/**
 * Created by Maibenben on 2017/5/4.
 */
$(function () {
    //跳转到本页之后读取cookie中是否有mid和token
    checkCookie();
    var mid=$.cookie("mid");
    var token=$.cookie("token");
    var important = "&mid=" + mid + "&token=" + token;
    var databack = false;
    //读取一下本地缓存
    var desc = localStorage.getItem("desc");//病情描述
    var doc_id = localStorage.getItem("doc_id");//医师id
    var member_id = localStorage.getItem("member_id");//就诊人id
    var work_time = localStorage.getItem("work_time");//预约时间
    var yudate = localStorage.getItem("yudate");
    var wholeTime=yudate + " " + work_time;
    $(".appoint_time").text(wholeTime);
    //进入页面显示上一个页面传过来的信息
    $.get(infoCheckURL + "?id=" + member_id + "&d_id=" + doc_id + important, function (res) {
        res = res.data;
        databack = true;
        $(".load").css("display", "none");
        $(".appoint_doctor").text(res.doctor.data.name);
        $(".appoint_office").text(res.doctor.data.depart.name);
        $(".appoint_price").text(res.doctor.data.free + "元");
        $(".mem-name").text(res.member.data.name);
        $(".mem-birthday").text(res.member.data.birthday);
        $(".mem-tel").text(res.member.data.mobile);
        $("#main").css("display", "block");
    });
    //是否接受约定
    $(".agree").children("input").click(function () {
        if ($(".agree").children("input").prop("checked")) {
            $(".footer").css("background", "rgb(47,178,159)");
        } else {
            $(".footer").css("background", "#ccc");
        }
    });
    //点击下一步跳转到支付订单
    $(".footer").on("click", function () {
        if (databack) {
            //是否接受约定
            if ($(".agree").children("input").prop("checked")) {
                //判断同一天同一人同一科室的预约
                $.get(checkindayURL,{m_id:member_id,d_id:doc_id,mark_time:wholeTime,mid:mid,token:token}, function (res) {
                    if(res.state==1){
                        //将预约信息保存起来
                        var dataPush = {
                            m_id: parseInt(member_id),
                            mid: $.cookie("mid"),
                            token: $.cookie("token"),
                            d_id: doc_id,
                            mark_time: $(".appoint_time").text(),
                            describe: desc,
                            pics: localStorage.getItem("base"),
                            money: parseInt($('.appoint_price').text())
                        };
                        databack = false;
                        $.post(infoAddURL, dataPush, function (res) {
                            databack = true;
                            //进入下一个页面
                            if (res.state) {
                                window.location.href = "purchase.html?aid=" + res.aid;
                            } else {
                                creatLayout(res.msg);
                            }
                        });
                    }else{
                        creatLayout(res.msg);
                    }
                });
            }
        } else {
            creatLayout("请稍等");
        }
    });
});